package com.longcai.service.impl.homepage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.entity.homepage.ClyjUserSearch;
import com.longcai.entity.homepage.GoodsClass;
import com.longcai.mapper.homegood.ClyjGoodsBrandMapper;
import com.longcai.mapper.homegood.ClyjGoodsHomepageMapper;
import com.longcai.mapper.homepage.ClyjUserSearchMapper;
import com.longcai.mapper.homepage.GoodsClassMapper;
import com.longcai.service.homepage.ClyjUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LWK
 * @since 2019-03-20
 */
@Service
public class ClyjUserSearchServiceImpl extends ServiceImpl<ClyjUserSearchMapper, ClyjUserSearch> implements ClyjUserSearchService {

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    @Autowired
    private ClyjGoodsBrandMapper clyjGoodsBrandMapper;

    @Autowired
    private ClyjUserSearchService clyjUserSearchService;

    @Autowired
    private ClyjGoodsHomepageMapper clyjGoodsHomepageMapper;

    /**
     * 查询类型所有子类
     *
     * @param name
     * @return
     */
    @Override
    public List findTypeTree(String name) {
        //默认查询出所有的一级类别
        QueryWrapper<GoodsClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        queryWrapper.isNull("p_id");
        queryWrapper.eq("type_status", "0");
        List<GoodsClass> typeList = this.goodsClassMapper.selectList(queryWrapper);
        List list = new ArrayList<>();
        //判断如果不是一级菜单直接去查下级
        if (typeList.size() == 0) {
            List<GoodsClass> goodsClassList = this.goodsClassMapper.select(name);
            for (GoodsClass goodsClass : goodsClassList) {
                list.add(goodsClass.getId());
                clyjUserSearchService.getChildren(goodsClass.getId(), list);
            }
        }
        //循环取出id
        for (GoodsClass goodsClass : typeList) {
            list.add(goodsClass.getId());
            getChildren(goodsClass.getId(), list);
        }
        return list;
    }

    @Override
    public List getChildren(String parentId, List list) {
        QueryWrapper<GoodsClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_id", parentId);
        List<GoodsClass> children = this.goodsClassMapper.selectList(queryWrapper);
        for (GoodsClass goodsClass : children) {
            list.add(goodsClass.getId());
            //查询所有子类
            getChildren(goodsClass.getId(), list);
        }
        return list;
    }


    /**
     * 搜索显示候选词
     *
     * @param keyvalue
     * @return
     */
    @Override
    public Map<String, Object> getSearchkey(String keyvalue) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            //关键字模糊查询品牌
            List clyjGoodsBrands = this.clyjGoodsBrandMapper.selectname(keyvalue);
            result.put("clyjGoodsBrands", clyjGoodsBrands);
            //关键字模糊查询类型
            List goodsClasses = this.goodsClassMapper.selectname(keyvalue);
            result.put("goodsClasses", goodsClasses);
            //关键字搜索商品名称
            List goods = this.clyjGoodsHomepageMapper.selectGoodsName(keyvalue);
            result.put("goods", goods);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            e.getMessage();
        }
        return null;
    }
}
